package com.along.game.action.manage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.along.framework.utils.StrFuncs;
import com.along.framework.web.BaseAction;
import com.along.game.model.MenuTree;
import com.along.game.model.SysMenu;
import com.along.game.service.SysMenuService;



/*
 * 系统菜单Action
 * Copyright 2011
 * @author alonglzb
 * @date 2011-08-1
 */
public class SysMenuAction extends BaseAction{
	
	@Autowired
	private SysMenuService sysMenuService;
	
	public String getSysMenuList(){
		try{
			String userId = "";
			//String userId = ReqFuncs.getStringParam(request, "");
			List sysMenuList = sysMenuService.getSysMenuByUser(userId);
			
			String jsTree = getJSTreeString(sysMenuList);
			this.struts2Util.getRequest().setAttribute("jsTree", jsTree);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "success";
	}
	
	/**
	 * 
	 * @param sysMenuList
	 * @return
	 */
	public ArrayList<MenuTree> makeMenuTree(List sysMenuList){
		ArrayList<MenuTree> menuTreeList = new ArrayList<MenuTree>();
		Iterator sMenuIt = sysMenuList.iterator(); 			
		while(sMenuIt.hasNext()){
			SysMenu sysMenu = (SysMenu)sMenuIt.next();
			MenuTree menuTree = new MenuTree();
			menuTree.setNodeId(StrFuncs.nvl(sysMenu.getMenuId()));
			menuTree.setNodeName(StrFuncs.nvl(sysMenu.getMenuName()));
			menuTree.setParentNodeId(StrFuncs.nvl(sysMenu.getMenuPid()));
			if("1".equals(StrFuncs.nvl(sysMenu.getMenuPid())))
			    menuTree.setParentFlag("Y");
			if(!"1".equals(StrFuncs.nvl(sysMenu.getMenuPid())))
			    menuTree.setNodeFlag("Y");
			menuTreeList.add(menuTree);
		}		
		
		return menuTreeList;
	}
	
	// 返回定义且生成页面树的 js 字符串   
	public String getJSTreeString(List sysMenuList){
		StringBuffer sbf = new StringBuffer(); 
		//SysUser tbSysUser = (SysUser)request.getSession().getAttribute(CommonConsts.LOGIN_USER);
		
		try{
		    //Iterator treeIt = sysMenuList.iterator();	      
		    // 定义js树对象   
		    sbf.append("dtree = new dTree(\"dtree\");"); 
		   // sbf.append("dtree.add(0,-1,'测试', 'test.do', '', 'MainFrame1');");
		    for(int i=0; i<sysMenuList.size(); i++){
		    	SysMenu sysMenu = (SysMenu)sysMenuList.get(i);
		    	String menuId = sysMenu.getMenuId()+"";
		    	String menuPId = sysMenu.getMenuPid()+"";
		    	String menuName = sysMenu.getMenuName();
		    	
		    	String menuURL = sysMenu.getMenuUrl()+"?menuId="+menuId+"";
		    	// 增加 js树结点 
		    	if(menuPId != null && "0".equals(menuPId)){
		    		  menuPId = "-1";  //根目录
		    	}
		    	sbf.append("dtree.add("+menuId+","+menuPId+",\""+menuName+"\",\""+menuURL+"\",\""+""+"\",\""+"MainFrame1"+"\");");  
		    } 
		    // 输出js树   
		    sbf.append("document.write(dtree);"); 
		   
		}catch(Exception e){
			e.printStackTrace();
		}
		
		 System.out.println("sbf == "+sbf.toString());
	    return sbf.toString();   
	}  
}
